package cn.supermarket.service.impl;

import cn.supermarket.ex.ServiceException;
import cn.supermarket.mapper.ReceivingMapper;
import cn.supermarket.pojo.dto.ReceivingDTO;
import cn.supermarket.pojo.entity.Receiving;
import cn.supermarket.pojo.vo.ReceivingSVO;
import cn.supermarket.pojo.vo.ReceivingVO;
import cn.supermarket.service.iservice.IReceivingService;
import cn.supermarket.utils.GetDateTime;
import cn.supermarket.utils.SnowFlakeGenerateIdWorker;
import cn.supermarket.web.State;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class ReceivingServiceImpl implements IReceivingService {

    @Autowired
    private ReceivingMapper receivingMapper;

    @Override
    public void addReceiving(ReceivingDTO receivingDTO) {
        Receiving receiving = new Receiving();
        BeanUtils.copyProperties(receivingDTO, receiving);
        SnowFlakeGenerateIdWorker autoId = new SnowFlakeGenerateIdWorker(2, 22);
        long supId = autoId.nextId();
        LocalDateTime now = LocalDateTime.now();
        receiving.setId(supId);
        receiving.setDate(now);
        receiving.setCreateDate(now);
        receiving.setUpdateDate(now);
        receiving.setIsCancel(0);
        receiving.setIsDisplay(1);
        receiving.setIsEnable(1);
        receiving.setDeliveryNote(autoId.nextId());
        receivingMapper.insertReceiving(receiving);
        System.out.println(receiving.toString());
    }

    @Override
    public void updateRemark(Long id, String remark) {
        ReceivingSVO remarkId = receivingMapper.getById(id);
        // 判断id是否存在
        if (remarkId == null) {
            throw new ServiceException(State.ERR_UNKNOWN_ERROR, "查询id不存在");
        }
        int rows = receivingMapper.updateRemark(id, remark);
        receivingMapper.updateRemark(id, LocalDateTime.now());
        if (rows == 0) {
            throw new ServiceException(State.ERR_UPDATE, "备注修改失败，请重新修改");
        }
    }

    @Override
    public void updateIsDisplay(Long id, Integer isDisplay) {
        ReceivingSVO isDisplayId = receivingMapper.getById(id);
        if (isDisplayId == null) {
            throw new ServiceException(State.ERR_UNKNOWN_ERROR, "查询id不存在");
        }
        if (isDisplayId.getIsDisplay().equals(isDisplay)) {
            throw new ServiceException(State.ERR_UPDATE, "修改失败，请重新尝试");
        }
        if (isDisplay == 1 || isDisplay == 0) {
            receivingMapper.updateIsDisplay(id, isDisplay);
            receivingMapper.updateDate(id, LocalDateTime.now());
        } else {
            throw new ServiceException(State.ERR_BAD_REQUEST, "修改参数错误，请重新尝试！");
        }
    }

    @Override
    public void updateIsCancel(Long id, Integer isCancel) {
        ReceivingSVO isCancelId = receivingMapper.getById(id);
        if (isCancelId == null) {
            throw new ServiceException(State.ERR_UNKNOWN_ERROR, "查询id不存在!");
        }
        if (isCancelId.getIsCancel().equals(isCancel)) {
            throw new ServiceException(State.ERR_UPDATE, "修改失败，请重新尝试...");
        }
        if (isCancel == 1 || isCancel == 0) {
            receivingMapper.updateIsCancel(id, isCancel);
            receivingMapper.updateDate(id, LocalDateTime.now());
        } else {
            throw new ServiceException(State.ERR_BAD_REQUEST, "修改参数错误，请重新尝试!");
        }
    }

    @Override
    public List<ReceivingVO> selectByOrderId(Long orderId) {
        List<ReceivingVO> list = receivingMapper.getByOrderId(orderId);
        if (list.isEmpty()) {
            throw new ServiceException(State.ERR_NOT_FOUND, "此订单号不存在，请重新输入正确单号！");
        }
        return list;
    }

    @Override
    public List<ReceivingVO> selectByDate(Date date) {

        GetDateTime getDateTime = new GetDateTime();

        LocalDateTime dateStart = getDateTime.getDateStart(date);
        LocalDateTime dateEnd = getDateTime.getDateEnd(date);

        List<ReceivingVO> list = receivingMapper.getByDate(dateStart, dateEnd);
        if (list.isEmpty()) {
            throw new ServiceException(State.ERR_NOT_FOUND, "该日期内没有提货信息，请重新查找！");
        }
        return list;
    }

    @Override
    public List<ReceivingVO> selectByConsignee(String returner) {
        List<ReceivingVO> list = receivingMapper.getByConsignee(returner);
        if (list.isEmpty()) {
            throw new ServiceException(State.ERR_NOT_FOUND, "没有该提货信息，请重新输入提货人");
        }
        return list;
    }
}
